package com.bai.boot.service.impl;

import com.bai.boot.mapper.UserMapper;
import com.bai.boot.pojo.User;
import com.bai.boot.service.IUserService;
import com.bai.boot.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import sun.security.util.Password;

import javax.swing.text.PasswordView;
import java.util.Date;

/**
 * @author 27471
 * Time : 2022/9/30 8:59
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public void reg(User user) {
        //判断用户知否被注册过
        User result = userMapper.findByUsername(user.getUsername());
        if (result != null) {
            //抛出异常
            throw new UsernameDuplicatedException("用户名被占用");
        }
        //数据补全
        user.setIsDelete(0);
        user.setCreatedUser(user.getUsername());
        user.setModifiedUser(user.getUsername());
        Date date = new Date();
        user.setCreatedTime(date);
        user.setModifiedTime(date);

        //执行业务功能
        Integer rows = userMapper.insert(user);
        if (rows != 1) {
            throw new InsertExption("在用户注册过程产生了不可知的异常");
        }
    }

    @Override
    public User login(String username, String password) {
        User result = userMapper.findByUsername(username);
        //获取数据库中加密的密码
        String oldPassword = result.getPassword();
        //密码md5加密
        String newMd5Password= DigestUtils.md5DigestAsHex(password.getBytes());
        String newoldPassword=DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        if (result==null){
            throw new UserNotExistException("用户不存在");
        } else if (!newMd5Password.equals(newoldPassword)){
            throw  new OriginalPasswordNotMatchException("用户密码错误");
        } else if (result.getIsDelete()==1){
            throw  new UserNotExistException("用户已被注销");
        }
        //返回需要的数据，提高数据
        User user = new User();
        user.setUid(result.getUid());
        user.setUsername(result.getUsername());
        user.setAvatar(result.getAvatar());
        return user;
    }

    @Override
    public void UpdatePasswordByUid(Integer uid,String username, String oldPassword, String newPassword) {
        User byUid = userMapper.findByUid(uid);
        if (byUid==null || byUid.getIsDelete()==1){
           throw  new UserNotExistException("用户不存在");
        }
        //原始密码和数据库密码比较
        String s = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        if (!byUid.getPassword().equals(s)){
            throw new OriginalPasswordNotMatchException("密码错误");
        }
        //将新密码设置到数据库，将新密码加密更新
        String s1 = DigestUtils.md5DigestAsHex(newPassword.getBytes());
        Integer integer = userMapper.UpdatePasswordByUid(uid, s1, username, new Date());
        if (integer!=1){
            throw new UpdateException("更新时产生未知的异常");
        }
    }

    @Override
    public void changeAvatar(Integer uid, String avatar, String username) {
         //查询当前的用户数据是否存在
        User byUid = userMapper.findByUid(uid);
        if (byUid==null||byUid.getIsDelete()==1){
            throw new UserNotExistException("用户数据不存在");
        }

        Integer integer = userMapper.updateAvatarByUid(uid, avatar, username, new Date());
        if (integer!=1){
            throw new UpdateException("更新用户头像产生未知异常");
        }

    }


}
